﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

using EFong.ServicesInterface;
using EFong.Contract;
using UIShare;
using System.Xml.Linq;

namespace EFong.View.Proxy
{
    public class BaseServiceProxy : ClientBase<IBaseService>, IBaseService
    {
        #region Constructor

        public BaseServiceProxy(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        { 
        }

        public BaseServiceProxy(InstanceContext callbackInstance) :
            base(callbackInstance)
        {
        }

        public BaseServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName) :
            base(callbackInstance, endpointConfigurationName)
        {
        }

        public BaseServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public BaseServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public BaseServiceProxy(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) :
            base(callbackInstance, binding, remoteAddress)
        {
        }

        public BaseServiceProxy(Binding binding, EndpointAddress remoteAddress) :
            base( binding, remoteAddress)
        {
        }

        private static BaseServiceProxy _instance;
        public static BaseServiceProxy GetInstance() 
        {
            if (_instance == null || _instance.State == CommunicationState.Faulted)
            {
                string url = string.Format("Http://{0}:{1}/{2}", ProxyMgt.IP, ProxyMgt.Port, "BaseService");

                EndpointAddress epa = new EndpointAddress(url);

                WSHttpBinding binding = new WSHttpBinding();
                //binding.ReliableSession.Enabled = true;
                binding.Security.Mode = SecurityMode.None;
                binding.SendTimeout = new TimeSpan(0, 1, 30);
                binding.MaxReceivedMessageSize = 65536000;
                binding.ReaderQuotas.MaxArrayLength = 65536000;
                binding.ReaderQuotas.MaxStringContentLength = 65536000;

                _instance = new BaseServiceProxy(binding, epa);
            }
            return _instance;
        }

        #endregion

        #region IBaseService 成员

        public ServiceResult<List<Currency>> GetAllCurrency()
        {
            var result = base.Channel.GetAllCurrency();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<Currency> GetCNYCurrency()
        {
            var result = base.Channel.GetCNYCurrency();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateExchangeRate(List<Currency> currencyList)
        {
            var result = base.Channel.UpdateExchangeRate(currencyList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<User> Login(string name, string password)
        {
            var result = base.Channel.Login(name, password);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<User>> GetAllUser()
        {
            var result = base.Channel.GetAllUser();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<User>> GetEffUser()
        {
            var result = base.Channel.GetEffUser();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewUser(User user)
        {
            var result = base.Channel.NewUser(user);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult ModifyUserPassword(string userName, string oldPassword, string newPassword) 
        {
            var result = base.Channel.ModifyUserPassword(userName,oldPassword,newPassword);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateUserList(List<User> userList)
        {
            var result = base.Channel.UpdateUserList(userList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Role>> GetAllRole()
        {
            var result = base.Channel.GetAllRole();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewRole(Role role)
        {
            var result = base.Channel.NewRole(role);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateRoleList(List<Role> roleList)
        {
            var result = base.Channel.UpdateRoleList(roleList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<HotelLevel>> GetAllHotelLevel() 
        {
            var result = base.Channel.GetAllHotelLevel();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<AuthorizedItem>> GetAllAuthorizedItem() 
        {
            var result = base.Channel.GetAllAuthorizedItem();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.CustomerHotelIntergralRate>> GetOriginalHotelIntergralRateList() 
        {
            var result = base.Channel.GetOriginalHotelIntergralRateList();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.WebUser>> GetAvailableWebUserList() 
        {
            var result = base.Channel.GetAvailableWebUserList();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<DateTime> GetDate() 
        {
            var result = base.Channel.GetDate();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult ImportData(XElement data)
        {
            var result = base.Channel.ImportData(data);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<XElement> ExportData(DateTime dateTime)
        {
            var result = base.Channel.ExportData(dateTime);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<DateTime> GetLatestExportDate()
        {
            var result = base.Channel.GetLatestExportDate();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        #endregion
    }
}
